package com.mlethe.android.demo.seattable.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mlethe.android.demo.R;
import com.mlethe.android.demo.seattable.activity.SeatTableActivity;
import com.mlethe.android.demo.seattable.base.BaseFragment;
import com.mlethe.android.demo.seattable.base.BasePresenter;
import com.mlethe.android.demo.seattable.entity.Hall;
import com.mlethe.library.recyclerview.adapter.BaseQuickAdapter;
import com.mlethe.library.recyclerview.viewholder.ViewHolder;
import com.mlethe.library.recyclerview.decoration.LinearItemDecoration;

import java.text.SimpleDateFormat;
import java.util.ArrayList;


public class MovieSessionFragment extends BaseFragment {

    RecyclerView recyclerView;

    private MovieFieldAdapter adapter;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            ArrayList<Hall> halls = bundle.getParcelableArrayList("halls");
            adapter.notifyItemInsert(halls);
        }
    }

    @Override
    protected void initView() {
        recyclerView = rootView.findViewById(R.id.movie_session_rv);
        adapter = new MovieFieldAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new LinearItemDecoration(getActivity()).setDrawable(R.drawable.line_grey_1dp).setSize(1));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_movie_session;
    }

    private class MovieFieldAdapter extends BaseQuickAdapter<Hall> {
        public MovieFieldAdapter() {
            super(R.layout.item_movie_session);
        }

        @Override
        protected void convert(@NonNull ViewHolder holder, Hall item, int position) {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            holder.setText(R.id.item_session_time_tv, sdf.format(item.getStartTime()))
                    .setText(R.id.item_session_end_time_tv, sdf.format(item.getEndTime()) + "散场")
                    .setText(R.id.item_session_type_tv, item.getMovieInfo().get(0).getMovieSubtitle())
                    .setText(R.id.item_session_position_tv, item.getHallName())
                    .setText(R.id.item_session_price_tv, item.getMarketPrice() + "元")
                    .setOnClickListener(R.id.item_session_selection_btn, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(getContext(), SeatTableActivity.class);
                            intent.putExtra("hall", item);
                            getContext().startActivity(intent);
                        }
                    });
        }
    }

}
